package com.zzk.lanouhn.myapplication1.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.activity.AllNewsDetailsActivity;
import com.zzk.lanouhn.myapplication1.adapters.ScrollRecyclerViewAdapter;
import com.zzk.lanouhn.myapplication1.intefaces.NewsItemCliclListener;
import com.zzk.lanouhn.myapplication1.modles.AllNews;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 新闻->B轮后(酷公司)页面
 * Created by lanouhn on 16/8/25.
 */
public class NewsAfterBFragment extends Fragment implements NewsItemCliclListener{

    private List<AllNews.Datas.DataBean> allNews=new ArrayList<>();;
    private List<AllNews.Datas.DataBean> temp=new ArrayList<>();
    private XRecyclerView scrollRecyclerView;
    private ScrollRecyclerViewAdapter adapter;
    private int page=20;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=LayoutInflater.from(getActivity()).inflate(R.layout.fragment_news_afterb,null);
        scrollRecyclerView= (XRecyclerView) view.findViewById(R.id.scroll_afterb_news);
        LinearLayoutManager manager=new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        scrollRecyclerView.setLayoutManager(manager);
        //上拉加载的动画效果
        scrollRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        //下拉刷新的动画效果
        scrollRecyclerView.setArrowImageView(R.drawable.ic_loading_rotate);
        scrollRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=20;
                getDataFromweb();
            }

            @Override
            public void onLoadMore() {
                page+=20;
                getDataFromweb();
            }
        });
        getDataFromweb();
        return view;
    }

    private void getDataFromweb() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String s=String.valueOf(page);
                String url="https://rong.36kr.com/api/mobi/news?pageSize="+s+"&columnId=102&pagingAction=up";
                OkHttpClient client=new OkHttpClient();
                Request request=new Request.Builder()
                        .url(url)
                        .build();

                Call call=client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                      String result=response.body().string();
                        Gson gson=new Gson();
                        temp=gson.fromJson(result,AllNews.class).getDatas().getData();
                        if (temp.size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                });
            }
        }).start();
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what){
                case 0:
                    //Toast.makeText(getActivity(), "请求数据失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    if (page == 20) {
                        allNews.clear();
                        allNews.addAll(temp);
                        adapter = new ScrollRecyclerViewAdapter(getActivity(), allNews);
                        adapter.setScrollRecyclerViewAdapter(NewsAfterBFragment.this);
                        scrollRecyclerView.setAdapter(adapter);
                        scrollRecyclerView.refreshComplete();
                        temp.clear();
                    } else {
                        allNews.addAll(temp);
                        adapter.notifyDataSetChanged();
                        scrollRecyclerView.loadMoreComplete();
                        temp.clear();
                    }


                    break;
              default:

                  break;
            }
        }
    };

    @Override
    public void onItemClick(int position) {
        Intent intent=new Intent(getActivity(), AllNewsDetailsActivity.class);

        Bundle bundle=new Bundle();
        bundle.putString("详情",allNews.get(position).getFeedId().toString());
        intent.putExtras(bundle);
        startActivity(intent);
    }
}
